<?php
/**
 * 云门店
 */
defined('IN_IA') or exit('Access Denied');
include("../addons/j_hmoney/include/fun/CLSS_jetsum.php");
include("../addons/j_hmoney/include/fun/jetsum_function.php");
class j_hmoneyModule extends WeModule {
	public function fieldsFormDisplay($rid = 0) {
		//要嵌入规则编辑页的自定义内容，这里 $rid 为对应的规则编号，新增时为 0
		global $_W;
		if (!empty($rid)) {
			$reply = pdo_fetch("SELECT * FROM ".tablename("j_hmoney_reply")." WHERE rid = :rid limit 1", array(':rid' => $rid));
		}

		load()->func('tpl');
		include $this->template('form');
	}

	public function fieldsFormValidate($rid = 0) {
		//规则编辑保存时，要进行的数据验证，返回空串表示验证无误，返回其他字符串将呈现为错误提示。这里 $rid 为对应的规则编号，新增时为 0
		return '';
	}

	public function fieldsFormSubmit($rid) {
		//规则验证无误保存入库时执行，这里应该进行自定义字段的保存。这里 $rid 为对应的规则编号
		global $_W, $_GPC;
		$reply_id=$_GPC["reply_id"];
		$insert = array(
			'weid'=> $_W['uniacid'],
			'gameid'=> $_GPC['gameid'],
			'gtype'=> intval($_GPC['gtype']),
			'status'=> $_GPC['status'],
			'title'=> $_GPC['title'],
		);
		if (empty($reply_id)) {
			$insert['rid']=$rid;
			$insert['status']=1;
			pdo_insert("j_hmoney_reply", $insert);
		} else {
			pdo_update("j_hmoney_reply", $insert, array('rid' => $rid));
		}
	}

	public function ruleDeleted($rid) {
		//删除规则时调用，这里 $rid 为对应的规则编号
		pdo_delete("j_hmoney_reply", array('rid'=>$rid));
	}
	
	public function settingsDisplay($settings) {
		global $_W, $_GPC;

        $isseting=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_systemsetting')." order by id asc limit 1");
        if(!$isseting){
            pdo_insert("j_hmoney_systemsetting",array("keycode"=>$settings["softkey"]));
        }
        if(!pdo_fieldexists('j_hmoney_systemsetting', 'appurl')) {
            pdo_query("ALTER TABLE ".tablename('j_hmoney_systemsetting')." ADD `appurl` varchar(300) NOT NULL DEFAULT '';");
        }
        if(!pdo_fieldexists('j_hmoney_systemsetting', 'elemdebug')) {
            pdo_query("ALTER TABLE ".tablename('j_hmoney_systemsetting')." ADD `elemdebug` tinyint(1) NOT NULL DEFAULT '0';");
        }
		if (checksubmit()) {
			$cfg = array();
			foreach($settings as $index=>$row){
				$cfg[$index]=$row;
			}
            $data=array(
                "elemdebug"=>$_GPC['elemdebug'],
                "elem_title"=>$_GPC['elem_title'],
                "elem_id"=>$_GPC['elem_id'],
                "elem_key"=>$_GPC['elem_key'],
                "elem_secret"=>$_GPC['elem_secret'],
            );
            if($_GPC['softkey'])$data["keycode"]=$_GPC['softkey'];

            load()->func('file');
            $dir_url="../attachment/j_hmoney/cert_2/";
            mkdirs($dir_url);
            $data['softurl']=$_GPC['softurl2'];
            if ($_FILES["softurl"]["name"]){
                if(file_exists($dir_url.$isseting["softurl"]))@unlink ($dir_url.$isseting["softurl"]);
                $data['softurl']=TIMESTAMP.".zip";
                move_uploaded_file($_FILES["softurl"]["tmp_name"],$dir_url.$data['softurl']);
            }
            $data['softmemberurl']=$_GPC['softmemberurl2'];
            if ($_FILES["softmemberurl"]["name"]){
                if(file_exists($dir_url.$isseting["softmemberurl"]))@unlink ($dir_url.$isseting["softmemberurl"]);
                $data['softmemberurl']=TIMESTAMP.".zip";
                move_uploaded_file($_FILES["softmemberurl"]["tmp_name"],$dir_url.$data['softmemberurl']);
            }

            $data['appurl']=$_GPC['appurl2'];
            if ($_FILES["appurl"]["name"]){
                if(file_exists($dir_url.$isseting["appurl"]))@unlink ($dir_url.$isseting["appurl"]);
                $data['appurl']=$_FILES["appurl"]["name"];
                move_uploaded_file($_FILES["appurl"]["tmp_name"],$dir_url.$data['appurl']);
            }

            if(count($data))pdo_update("j_hmoney_systemsetting", $data);
            if($_GPC['softkey2'])$data["keycode"]=$_GPC['softkey'];
			$cfg['floder']=$_GPC['floder'] ? $_GPC['floder'] : "default" ;
            $cfg["keycode"]=$_GPC['softkey2'];



            $cfg["isymf"]=$_GPC['isymf'];
            $cfg["customerid"]=$_GPC['customerid'];
            $cfg["customerkey"]=$_GPC['customerkey'];
            $cfg["customerurl"]=$_GPC['customerurl'];
            if ($_W['isfounder']){
                $cfg["limitnum"]=$_GPC['limitnum'];
            }
            if ($this->saveSettings($cfg))message('保存成功', 'refresh');
        }
		include $this->template('setting');
	}

}